package io.renren.controller;

import com.alibaba.fastjson.JSON;
import io.renren.common.utils.R;
import io.renren.common.utils.RedisUtils;
import io.renren.dto.QualDto;
import io.renren.entity.QualHonorEntity;
import io.renren.entity.RichtextContentEntity;
import io.renren.service.impl.QualHonorServiceImpl;
import io.renren.service.impl.RichtextContentServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/content")
@Api(tags = "查询内容接口")
public class ContentController {

    @Autowired
    private RichtextContentServiceImpl richtextContentService;

    @Autowired
    private QualHonorServiceImpl qualHonorService;

    /**
     * params 中含以下参数
     * 有categoryCode 内容类型
     * limit 每页大小
     * page 页码
     *
     * @param params
     * @return 返回结果
     */
    @PostMapping("list")
    @ApiOperation("获取内容列表")
    public R list(@RequestBody Map<String, Object> params) {
        try {
            if (params.get("categoryCode") == null) {
                return R.error("内容类型不能为空");
            }
            params.put("sidx", "create_time");
            params.put("order", "desc");

            return R.ok().put("data", richtextContentService.querList(params));
        } catch (Exception e) {
            return R.error(9999, "查询内容列表发生异常");
        }
    }

    @Autowired
    RedisUtils redisUtils;

    @PostMapping("get")
    @ApiOperation("获取内容")
    public R get(@RequestBody Map<String, Object> params) {
        try {
            if (params.get("id") == null) {
                return R.error("内容编号不能为空");
            }
            String key = "content_" + params.get("id").toString();
            String values = redisUtils.get(key);
            if (!StringUtils.isEmpty(values)) {
                return R.ok().put("data", JSON.parseObject(values, RichtextContentEntity.class));
            }
            RichtextContentEntity contentEntity = richtextContentService.getById(Long.valueOf(String.valueOf(params.get("id"))));
            if (contentEntity != null && contentEntity.getId() != null && contentEntity.getId().longValue() > 0) {
                redisUtils.set(key, JSON.toJSONString(contentEntity), 300);
            }
            return R.ok().put("data", contentEntity);
        } catch (Exception e) {
            return R.error(9999, "查询内容详情发生异常");
        }
    }

    @PostMapping("getQualOrHonor")
    @ApiOperation("获取集团荣誉或者集团资质")
    public R getQualOrHonor(@RequestBody Map<String, Object> params) {
        try {
            if (params.get("type") == null) {
                return R.error("查询类型不能为空");
            }
            String key = "content_" + params.get("type").toString();
            String values = redisUtils.get(key);
            if (!StringUtils.isEmpty(values)) {
                return R.ok().put("data", JSON.parseObject(values, QualDto.class));
            }
            List<QualDto> qualDtos = new ArrayList<>();
            List<QualHonorEntity> qualHonorEntities = qualHonorService.getList(params);
            if (qualHonorEntities != null && !qualHonorEntities.isEmpty()) {
                for (QualHonorEntity entity : qualHonorEntities) {
                    QualDto qualDto = new QualDto();
                    qualDto.setName(entity.getName());
                    qualDto.setImageUrl(entity.getImageUrl());
                    qualDto.setSort(entity.getSort());
                    qualDtos.add(qualDto);
                }
            }
            if (qualDtos != null && !qualDtos.isEmpty()) {
                redisUtils.set(key, JSON.toJSONString(qualDtos));
            }
            return R.ok().put("data", qualDtos);
        } catch (Exception e) {
            return R.error(9999, "获取集团荣誉或者集团资质发生异常");
        }
    }





}
